clear all
set more off
capture log close
********************************************************************************
*log using "$logfiles/analysis_2sls_`c(current_date)'.log", replace

use "$covidclean/smscovid_clean.dta", clear

***Keep relevant observations: consent taken and above 18 years of age, non-missing treatment arms
	keep if consent==1 & age>=18
	keep if ~missing(treatment_arm)
	count //3964

***HOUSEKEEPING - BEHAVIOR TREATMENTS 
	
	***Create dummies with treatment arms interacted with behavior
		foreach v in treatment_pooled {
			gen `v'_sd = `v'*behavior_sd
			gen `v'_hw = `v'*behavior_hw
		}
	
	***Row titles
		global rtitle_pooled "Treatment - SD \ "" \ "" \ Treatment - HW \ "" \ "" "
		
********************************************************************************
***MAIN ITT REGRESSIONS
********************************************************************************

***ADMIN MEASURE - WHETHER SMS WAS DELIVERED TO THE PHONE OR NOT

	***Generate admin delivered variable
	
		gen delivered = delivered_count>0
		replace delivered = . if missing(delivered_count)
		replace delivered = 0 if treatment_pooled==0
		tab delivered treatment_pooled, mi
		
		foreach v in $treat_pool_sd $treat_pool_hw {
			gen `v'_d = `v'*delivered
		}
	
	***Regressions
	
		foreach v of varlist $keysdhw {
		    
			*Control complier mean
			mean `v' if treatment_pooled==0 & $sample
			mat mean = r(table)
			local c_mean : di %9.2f mean[1,1]
		    
			noi ivreghdfe `v' (treatment_pooled_sd_d treatment_pooled_hw_d = $treat_pool_sd $treat_pool_hw) if $sample, a($studycontrols $covariates) r
		
			local tcount: word count $treat_pool_sd $treat_pool_hw	
			mat b = e(b)
			mat variance = e(V)
			local df = e(df_r)
			forval i = 1/`tcount' {
				local b_`i' = b[1,`i'] //beta
				local se_`i' = sqrt(variance[`i',`i']) //asymptotic s.e.
				local a_p_`i' = 2*(ttail(`df',abs(`b_`i''/`se_`i''))) //asymptotic p-value
				local p_`i' = 0 //exact p-value
			}
	
			local n : di %9.0fc e(N)
			local r2_a : di %9.2f e(r2_a)
				
			*Randomization inference using RI dataset created
			preserve
    			forval batch = 1/$nbatch {
				   use if batch == `batch' using "$rianalysis/ri_data_final", clear
    			   noi di "Running TOT-pooled for `v' for batch - `batch' at time: `c(current_time)'"
				   ***Generate admin delivered variable
				   gen delivered = delivered_count>0
				   replace delivered = . if missing(delivered_count)
				   replace delivered = 0 if treatment_pooled==0
				   foreach var in $treat_pool_sd $treat_pool_hw {
						gen `var'_d = `var'*delivered
				   }
				   ***Regressions
    			   ivreghdfe `v' (treatment_pooled_sd_d treatment_pooled_hw_d = $treat_pool_sd $treat_pool_hw) if $sample, a($studycontrols $covariates) r
    			   forval i = 1/`tcount' {
            		   local ri_b_`i' = e(b)[1,`i']
            		   *One-tailed comparison
			           if `ri_b_`i'' >= `b_`i'' local p_`i' = `p_`i'' + 1 
				    }  
			    } 
			restore
				
			*Compute Exact p-values 
			forval i = 1/`tcount' {
				local p_`i' = `p_`i''/$nbatch
			}
				
			*Imputing coefficients, asymptotic and exact p-values in the output matrix
			mat `v'_tot = J(`tcount',3,.)
			forval i = 1/`tcount' {
				mat `v'_tot[`i',1] = round(`b_`i'',0.001)
				mat `v'_tot[`i',2] = round(`se_`i'',0.001)
				mat `v'_tot[`i',3] = round(`p_`i'',0.001)
			}
			
			
			*Statistical significance
			mat stars = J(`tcount',3,.)
			forval i = 1/`tcount' {
				mat stars[`i',2] = 0
				mat stars[`i',3] = 0
				if (`a_p_`i'' < 0.1)  mat stars[`i',1] = 1
				else mat stars[`i',1] = 0
				if (`a_p_`i'' < 0.05) mat stars[`i',1] = 2 
				if (`a_p_`i'' < 0.01) mat stars[`i',1] = 3 
			}
				
			*Save matrix
			matsave `v'_tot, replace saving path($tables)
			*matload `v'_tot, path($tables) saving
				
			*Output 
			frmttable, statmat(`v'_tot) store(`v'_tot) substat(2) annotate(stars) asymbol(*,**,***) rtitle($rtitle_pooled) /// 
			addrows("Adjusted $ R^2$", `r2_a' \ N, "`n'" \ "Control Mean", `c_mean') sdec(3,3,3)
		    
		}

***SELF-REPORTED MEASURE - WHETHER ANY COVID-RELATED SMS WAS RECEIVED BY THE RESPONDENT OR NOT	

	tab smsreceive treatment_pooled

	***Create delivered variable for each target behavior
		foreach var in $treat_pool_sd $treat_pool_hw {
			gen `var'_s = `var'*smsreceive
		}	

	***Regressions
	
	qui	foreach v of varlist $keysdhw {
	    
			*Control complier mean
			mean `v' if treatment_pooled==0 & smsreceive==1 & $sample
			mat mean = r(table)
			local c_mean : di %9.2f mean[1,1]
		    
			noi ivreghdfe `v' (treatment_pooled_sd_s treatment_pooled_hw_s = $treat_pool_sd $treat_pool_hw) if $sample, a($studycontrols $covariates) r
		
			local tcount: word count $treat_pool_sd $treat_pool_hw	
			mat b = e(b)
			mat variance = e(V)
			local df = e(df_r)
			forval i = 1/`tcount' {
				local b_`i' = b[1,`i'] //beta
				local se_`i' = sqrt(variance[`i',`i']) //asymptotic s.e.
				local a_p_`i' = 2*(ttail(`df',abs(`b_`i''/`se_`i''))) //asymptotic p-value
				local p_`i' = 0 //exact p-value
			}
				
			local n : di %9.0fc e(N)
			local r2_a : di %9.2f e(r2_a)
				
			*Randomization inference using RI dataset created
			preserve
    			forval batch = 1/$nbatch {
				   use if batch == `batch' using "$rianalysis/ri_data_final", clear
    			   noi di "Running IV-pooled for `v' for batch - `batch' at time: `c(current_time)'"
				   ***Create delivered variable for each target behavior
					foreach var in $treat_pool_sd $treat_pool_hw {
						gen `var'_s = `var'*smsreceive
					}
				   ***Regressions
    			   ivreghdfe `v' (treatment_pooled_sd_s treatment_pooled_hw_s = $treat_pool_sd $treat_pool_hw) if $sample, a($studycontrols $covariates) r
    			   forval i = 1/`tcount' {
            		   local ri_b_`i' = e(b)[1,`i']
            		   *One-tailed comparison
			           if `ri_b_`i'' >= `b_`i'' local p_`i' = `p_`i'' + 1 
				    }  
			    } 
			restore
				
			*Compute Exact p-values 
			forval i = 1/`tcount' {
				local p_`i' = `p_`i''/$nbatch
			}
				
			*Imputing coefficients, asymptotic and exact p-values in the output matrix
			mat `v'_iv = J(`tcount',3,.)
			forval i = 1/`tcount' {
				mat `v'_iv[`i',1] = round(`b_`i'',0.001)
				mat `v'_iv[`i',2] = round(`se_`i'',0.001)
				mat `v'_iv[`i',3] = round(`p_`i'',0.001)
			}
			
			
			*Statistical significance
			mat stars = J(`tcount',3,.)
			forval i = 1/`tcount' {
				mat stars[`i',2] = 0
				mat stars[`i',3] = 0
				if (`a_p_`i'' < 0.1)  mat stars[`i',1] = 1
				else mat stars[`i',1] = 0
				if (`a_p_`i'' < 0.05) mat stars[`i',1] = 2 
				if (`a_p_`i'' < 0.01) mat stars[`i',1] = 3 
			}
				
			*Save matrix
			matsave `v'_iv, replace saving path($tables)
			*matload `v'_iv, path($tables) saving
				
			*Output 
			frmttable, statmat(`v'_iv) store(`v'_iv) substat(2) annotate(stars) asymbol(*,**,***) rtitle($rtitle_pooled) /// 
			addrows("Adjusted $ R^2$", `r2_a' \ N, "`n'" \ "Control Mean", `c_mean') sdec(3,3,3)
		    
		}
		
********************************************************************************
***COMPILE OUTPUTS
********************************************************************************

	*First compile results for TOT (admin), then for IV (self-reported)
	qui foreach v of varlist $keysdhw {	
		outreg, replay(pooled_tot) merge(`v'_tot) store(pooled_tot) nodisplay
		outreg, replay(pooled_iv) merge(`v'_iv) store(pooled_iv) nodisplay
			    
	}
	outreg, replay(pooled_tot) ///
		ct("", "Distancing", "", "Handwashing", "" \ "", "Know", "Act", "Know", "Act" \ "\bf{Panel A: TOT Regressions (delivery reports)}", "", "", "", "") ///
		addrows("", "", "", "", "" \ "\bf{Panel B: IV Regressions (self-reported receipt)}", "", "", "", "" \ "", "", "", "", "") 	
	
	***Globals for outputs
	global font "basefont(scriptsize) statfont(scriptsize; scriptsize; scriptsize; scriptsize) rtitlfont(scriptsize; scriptsize; scriptsize; scriptsize) ctitlfont(scriptsize) notefont(scriptsize)"
	global lines "hlines(1{0}1;{0};1001{0};1{0}1001)"
		
	***Display all results
	cd "$tables"
	outreg using key_2sls, replay(pooled_tot) append(pooled_iv) replace tex fr multicol(1,2,2;1,4,2;3,1,5;14,1,5) $font note("") $lines
	

exit, clear
